<?php

namespace App\service;

use App\Models\GrIntegral;
use App\Models\GrUser;
use Illuminate\Support\Facades\Redis;

class Server
{
    /**
     * 获取用户本月连续签到天数
     * @param $user_id
     * @param $toYear
     * @param $toMonth
     * @param $today
     * @return int|mixed
     */
    public static function continuitySign($user_id, $toYear, $toMonth, $today, $get_sign = null,$today_is_sign=3)
    {

        $sign_all = '';
        for ($num = $today; $num > 0; $num--) {
            $number = Redis::getBit($user_id . ':' . $toYear . $toMonth, $num);
            $sign_all = $sign_all . "," . $number;
        }
        $sign_data = explode(',', strrev($sign_all));//本月截止本天外所有签到数据转换为数组
        unset($sign_data[$today]);//字符串分割后删除多余的逗号
        if ($get_sign == 1) {
            return $sign_data;
        }
        if($today_is_sign==0){
            //减1的原因是因为数据库没有数据说明此人在本月从未签到过那么需要将今天的签到数据去除否则今天未签到为0直接判断为断签
            unset($sign_data[$today-1]);
            $continuity_day = 0;//连续签到的天数
            foreach ($sign_data as $val) {
                if ($val == 1) {
                    $continuity_day = $continuity_day + 1;//记录连续签到的天数
                } else if ($val == 0) {
                    $continuity_day = 0;//如果签到中断那么就重置连续签到次数
                }
            }
            return $continuity_day;
        }

        $continuity_day = 0;//连续签到的天数
        foreach ($sign_data as $val) {

            if ($val == 1) {
                $continuity_day = $continuity_day + 1;//记录连续签到的天数
            } else if ($val == 0) {
                $continuity_day = 0;//如果签到中断那么就重置连续签到次数
            }
        }
        return $continuity_day;
    }

    /**
     * 用户积分流水
     * @param $user_id
     * @param $integral_number
     * @param $integral_type
     * @param $is_Increase
     * @return mixed
     */
    public static function createIntegral($user_id, $integral_number, $integral_type, $is_Increase)
    {

        //记录用户签到补签的积分流水
        return GrIntegral::create([
            'user_id' => $user_id,
            'integral_number' => $integral_number, //积分
            'integral_type' => $integral_type,//来源
            'is_Increase' => $is_Increase//增减类型
        ]);
    }

    /**
     * 用户积分奖励
     * @param $continuity_day
     * @param $user_id
     * @param $month_end
     * @param null $is_supplement
     * @return bool|mixed
     */
    public static function sign_reward($continuity_day, $user_id, $month_end, $is_supplement = null,$original_sign=null)
    {
        $user_data = GrUser::where('id', $user_id)->first();
        $user_integral = $user_data->integral;
        if ($continuity_day == 7) {
            if ($is_supplement == 1) {
                GrUser::where('id', $user_id)->update(['integral' => $user_integral + 27]);//补签满七天励50积分-30积分加本来的7积分
                return self::createIntegral($user_id, 27, 0, 0);
            }
            GrUser::where('id', $user_id)->update(['integral' => $user_integral + 57]);//满七天奖励50积分 加上本来的积分
            return self::createIntegral($user_id, 57, 0, 0);
        }
        if ($continuity_day == 14) {
            if ($is_supplement == 1) {
                GrUser::where('id', $user_id)->update(['integral' => $user_integral - 16]);
                return self::createIntegral($user_id, 16, 2, 1);
            }
            GrUser::where('id', $user_id)->update(['integral' => $user_integral + $continuity_day]);
            self::createIntegral($user_id, 14, 0, 0);
            dd('额外赠送优惠券');
        }
        if ($continuity_day == $month_end) {
            if ($is_supplement == 1) {
                if ($continuity_day == 30) {
                    return true;
                }
                $source = $continuity_day - 30; //连续签到奖励积分-补签所用积分
                if ($source < 1) {      //   因为每月的次数不唯一 所以增减状态不能确定
                    $is_Increase = 2;
                } else {
                    $is_Increase = 1;
                }
                self::createIntegral($user_id, $source, 2, $is_Increase);
                return GrUser::where('id', $user_id)->update(['integral' => $user_integral + $source]);
            }
            GrUser::where('id', $user_id)->update(['integral' => $user_integral + $continuity_day]);
            dd('额外获取5元通用优惠券');
        }
        if ($continuity_day == 0) { //说明第一次签到 使用连续签到的次数做积分奖励如果第一次签到就赋值为1
            $continuity_day = 1;
        }
        if($is_supplement==1){
            if($original_sign==$continuity_day){ //补签前与补签后连续签到次数没有发生变化所以给1积分
                GrUser::where('id', $user_id)->update(['integral' => $user_integral - 29]);//
                return Server::createIntegral($user_id, 29, 2, 1);
            }
            $source=$continuity_day-30;
            GrUser::where('id', $user_id)->update(['integral' => $user_integral + $source]);//
            return Server::createIntegral($user_id, $source, 2, 1);
        }
        GrUser::where('id', $user_id)->update(['integral' => $user_integral + $continuity_day]);//
         Server::createIntegral($user_id, $continuity_day, 0, 0);
    }
}
